<?php
	require_once PATH_OBJECT.'dataObject.php';
	
class dataObject_cotisation extends dataObject
{

    /* @desc    Identifiant de la cotisation
     * @var     int
     */
    public $_IDENTIFIANT_COTIS = 0;	  	 
    
	/* @desc    Identifiant de la cotisation
     * @var     DATE
     */
    public $_DATE_DEBUT_COTIS = '';	  
    
	/* @desc    Identifiant de la cotisation
     * @var     DATE
     */
    public $_DATE_FIN_COTIS = '';	
      
	/* @desc    Identifiant de la cotisation
     * @var     int
     */
    public $_NUM_AVENANT_COTIS 	= 0;
    
	/* @desc    Identifiant de la cotisation
     * @var     float
     */
    public $_MNT_BASE_COTIS = 0;
    
    
	/* @desc    Identifiant de la cotisation
     * @var     float
     */
    public $_MNT_FRAIS_ECHEANCE_COTIS = 0;
    
	/* @desc    Identifiant de la cotisation
     * @var     float
     */
    public $_MNT_DROITS_ENTREE_COTIS = 0;
    
	/* @desc    Identifiant de la cotisation
     * @var     int
     */
    public $_IDENTIFIANT_CONTRAT = 0;

    
  	public function __construct( $iId=0 )
    {
       parent::__construct();

       if ( $iId != 0 )
       {
            $sQuery = "SELECT   *   /*+ dataObject_cotisation::__construct */  
                       FROM cotisation 
            		   WHERE Identifiant_cotisation = ".$iId;
            
            $aRows =  self::$_oConnexion->query($sQuery);
	  		$aRow = $aRows[0];
            $this->_IDENTIFIANT_COTIS = $iId;
			$this->_DATE_DEBUT_COTIS = $aRow['Date_debut_cotisation'];
			$this->_DATE_FIN_COTIS 	= $aRow['Date_fin_cotisation'];
			$this->_NUM_AVENANT_COTIS = $aRow['Numero_avenant_cotisation'];
			$this->_MNT_BASE_COTIS = $aRow['Montant_base_cotisation'];
			$this->_MNT_FRAIS_ECHEANCE_COTIS = $aRow['Montant_frais_echeance_cotisation'];
			$this->_MNT_DROITS_ENTREE_COTIS = $aRow['Montant_droits_entree_cotisation'];
			$this->_IDENTIFIANT_CONTRAT = $aRow['Identifiant_contrat'];
			 
 
       }
    }
    /**
     * récupere la table cotisation selon le(s) criter(s) précisés ou pas 
	 */
    function cotisation_select( $sCritere = null )
    {
        $aRow = false;
    	$sQuery = "SELECT   *    
                   FROM cotisation 
            	   WHERE 1=1 
            	   {$sCritere}";
        $aRow =  self::$_oConnexion->query($sQuery);

        return $aRow;
    }
    
    //detail dernier cotisation
    public function cotisation_detailCotisation($sCritere)
    {
    	$aRow = false;
    	$sQuery ="SELECT Identifiant_cotisation, Identifiant_contrat,
    			 Date_debut_cotisation, Date_fin_cotisation, Montant_base_cotisation,
    			 Montant_frais_echeance_cotisation, Montant_droits_entree_cotisation
				 FROM cotisation where Identifiant_contrat='".$sCritere."'
				 ORDER BY Identifiant_cotisation DESC
				 LIMIT 1 "	;
		$aRow =  self::$_oConnexion->query($sQuery);
		return $aRow;
    	
    }
    
    
    //detail dernier cotisation:dont impayee
    public function cotisation_detailCotisationImpEcheance($sIdcontrat, $idLastCotisation)
    {
    	$aRow = false;
    	$sQuery ="SELECT SUM(Montant_echeance) as Dont_impayee
					FROM echeancier, cotisation 
					where Etat_echeance = 'I' 
					 AND Identifiant_contrat= '".$sIdcontrat."' 
					 AND cotisation.Identifiant_cotisation = echeancier.Identifiant_cotisation
					 AND cotisation.Identifiant_cotisation = '".$idLastCotisation."' 
					ORDER BY cotisation.Identifiant_cotisation DESC ";
					//LIMIT 1"
		$aRow =  self::$_oConnexion->query($sQuery);
		return $aRow[0]['Dont_impayee'];
    	
    }
 //detail dernier cotisation:montant cotisation emis
    public function cotisation_detailCotisationEmisEcheance($sIdcontrat, $idLastCotisation)
    {
    	$aRow = false;
    	$sQuery ="SELECT 
    			SUM(Montant_echeance) as cotisation_emise
				FROM echeancier,cotisation where Etat_echeance IN('R','A','I') AND
				Identifiant_contrat="."'".$sIdcontrat."'"."".
				"and cotisation.Identifiant_cotisation=echeancier.Identifiant_cotisation
				AND cotisation.Identifiant_cotisation = '".$idLastCotisation."' 
				ORDER BY cotisation.Identifiant_cotisation DESC
				LIMIT 1"
				;
		$aRow =  self::$_oConnexion->query($sQuery);
		return $aRow[0]['cotisation_emise'];
    	
    }

	public function cotisation_insert()
    {
    	
    	$sQuery = "insert INTO cotisation (
    			Date_debut_cotisation,
				Date_fin_cotisation,
				Numero_avenant_cotisation ,
				Montant_base_cotisation,
				Montant_frais_echeance_cotisation,
				Montant_droits_entree_cotisation,
				Identifiant_contrat
    			) values (
    			".self::$_oConnexion->date($this->_DATE_DEBUT_COTIS).",
    			".self::$_oConnexion->date($this->_DATE_FIN_COTIS).",   
    			$this->_NUM_AVENANT_COTIS,
    			$this->_MNT_BASE_COTIS,
    			$this->_MNT_FRAIS_ECHEANCE_COTIS,
    			$this->_MNT_DROITS_ENTREE_COTIS,
    			$this->_IDENTIFIANT_CONTRAT
    			)";
    	
        	$bId = self::$_oConnexion->execute($sQuery);
    	return $bId;
    }
    
    public function cotisation_update()
    {
    	
    	$sQuery = "UPDATE cotisation SET 
    	
	    		WHERE Identifiant_cotisation 	= ".$this->_IDENTIFIANT_COTIS;

    	return self::$_oConnexion->execute($sQuery);
    }
    
    public function cotisation_getAvenant($IdContrat)
    {
    	$sQuery = "SELECT max(Numero_avenant_cotisation) as Max_Numero_avenant_cotisation 
    		FROM cotisation 
    		WHERE Identifiant_contrat='".$IdContrat."'";
    	
    	$aRow = self::$_oConnexion->query($sQuery);
    	
    	return $aRow[0]['Max_Numero_avenant_cotisation'];
    }
    
}
?>